package thread;

import org.omg.PortableInterceptor.INACTIVE;

import java.util.*;


public class Test {
    public static void main(String[] args)  {
        ArrayList<Integer> arrayList = new ArrayList<>();
//        A a = new A(arrayList);
//        List<Integer> integers = Collections.synchronizedList(list);
        Thread t1 = new Thread(){
            public   void run(){ //synchronized
                for (int i = 0; i < 1000; i++) {
                    synchronized (arrayList){
                        arrayList.add(i);
                    }

       }
            }
        };
        Thread t2 = new Thread(){
            public   void run(){  //synchronized
                for (int i = 0; i < 1000; i++) {
                    synchronized (arrayList){
                        arrayList.add(i);
                    }

                }
            }
        };
        t1.start();
        t2.start();

        try {
            Thread.sleep(1000);//阻塞主线程的目的是等待上述两个线程执行完毕
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(arrayList.size());
    }
}
//class A{
//    private ArrayList<Integer> arrayList;
//
//    public A(ArrayList<Integer> arrayList){
//        this.arrayList = arrayList;
//    }
//    public synchronized  void add(){
//        for (int i = 0; i < 1000; i++) {
//            arrayList.add(i);
//        }
//    }
//}